<?php
declare(strict_types=1);

namespace SixShop\Message\Entity;

use think\Model;

/**
 * 系统公告实体类
 */
class AnnouncementEntity extends Model
{
    // 设置表名
    protected $name = 'message_announcements';
    
    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'title'       => 'string',
        'content'     => 'string',
        'status'      => 'int',
        'type'        => 'int',
        'start_time'  => 'int',
        'end_time'    => 'int',
        'create_time' => 'int',
        'update_time' => 'int',
        'delete_time' => 'int',
    ];
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    
    // 软删除
    protected $deleteTime = 'delete_time';
    
    /**
     * 获取公告类型文本
     */
    public function getTypeTextAttr()
    {
        $types = [
            1 => '普通公告',
            2 => '重要公告',
            3 => '紧急公告',
        ];
        
        return $types[$this->type] ?? '未知类型';
    }
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr()
    {
        $statuses = [
            0 => '禁用',
            1 => '启用',
        ];
        
        return $statuses[$this->status] ?? '未知状态';
    }
    
    /**
     * 判断公告是否有效
     */
    public function isValid()
    {
        $now = time();
        
        // 检查状态和时间范围
        return $this->status == 1 
            && ($this->start_time == 0 || $this->start_time <= $now)
            && ($this->end_time == 0 || $this->end_time >= $now);
    }
}
